package com.demo2do.fms.admin.web.controller.data.business;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.report.ExportType;
import com.demo2do.fms.admin.entity.report.data.FacilityDistanceReportSearch;
import com.demo2do.fms.admin.entity.search.data.FacilityDistanceSearch;
import com.demo2do.fms.admin.service.data.FacilityDistanceService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.data.business.FacilityDistance;

/**
 * 行驶公里数查询
 * 
 * @author Ark
 *
 */
@Controller
@RequestMapping("/facility-distance-queries")
@MenuSetting("menu-data")
public class FacilityDistanceQueryController {

    @Autowired
    @Qualifier("facilityDistanceService")
    private FacilityDistanceService facilityDistanceService;
    
    /**
     * 列表
     * 
     * @param orgIds
     * @param page
     * @return
     */
    @MenuSetting("submenu-facility-distance-query")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView list(@Secure(property = "orgIds") List<Long> orgIds, Page page){
       
        ModelAndView modelAndView = new ModelAndView("data/business/facility-distance-query/facility-distance-query-list");
        
        int totals = facilityDistanceService.count(orgIds);
        if(totals > 0) {
            
            List<FacilityDistance> facilityDistances = facilityDistanceService.list(orgIds, page.with(totals));
         
            modelAndView.addObject("page", page);
            modelAndView.addObject("facilityDistances", facilityDistances);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param orgIds
     * @return
     */
    @MenuSetting("submenu-facility-distance-query")
    @RequestMapping(value = "search", method = RequestMethod.GET)
    public ModelAndView search(@Secure(property = "orgIds") List<Long> orgIds){
        
        ModelAndView modelAndView = new ModelAndView("data/business/facility-distance-query/facility-distance-query-search");
        
        modelAndView.addObject("orgIds", orgIds);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param facilityDistanceSearch
     * @param page
     * @param request
     * @return
     */
    @MenuSetting("submenu-facility-distance-query")
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    public ModelAndView onSearch(FacilityDistanceSearch facilityDistanceSearch, Page page, HttpServletRequest request){
        
        ModelAndView modelAndView = new ModelAndView("data/business/facility-distance-query/facility-distance-query-list");
        
        int totals = facilityDistanceService.count(FacilityDistance.class, facilityDistanceSearch.getFilter());
        
        if(totals > 0){
            
            List<FacilityDistance> facilityDistances = facilityDistanceService.search(FacilityDistance.class, facilityDistanceSearch, page.with(totals));
            
            modelAndView.addObject("facilityDistances", facilityDistances);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "filter","page"));
        
        }
        
        return modelAndView;
    }
    
    /**
     * 输出当前页记录
     * 
     * @param orgIds
     * @param page
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export(@Secure(property = "orgIds") List<Long> orgIds, Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("data/business/facility-distance-query-export.xlsx"));
        
        // 1. list result 
        int totals = facilityDistanceService.count(orgIds);
        if(totals > 0) {
            List<FacilityDistance> facilityDistances = facilityDistanceService.list(orgIds, page.with(totals));
            modelAndView.addObject("facilityDistances", facilityDistances);
        }
        
        // 2. attribute
        modelAndView.addObject("type", ExportType.CURRENT);
        
        // 3. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
    /**
     * 导出所有记录
     * 
     * @param search
     * @param orgIds
     * @return
     */
    @RequestMapping(value = "/export-all", method = RequestMethod.GET)
    public ModelAndView exportAll(FacilityDistanceReportSearch search, @Secure(property = "orgIds") List<Long> orgIds) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("data/business/facility-distance-query-export.xlsx"));
        
        // 1. list result
        if (CollectionUtils.isEmpty(search.getOrganizationIds())) {
            search.setOrganizationIds(orgIds);
        }
        List<Map<String, Object>> results = facilityDistanceService.exportAll(search);
        modelAndView.addObject("results", results);
        
        // 2. attribute
        modelAndView.addObject("type", ExportType.ALL);
        
        // 3. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
}
